﻿


//自定完成分隔
function auto_split(val)
{
    return val.split(/,\s*/);
}
//自定完成组合
function auto_extractLast(term)
{
    return split(term).pop();
}

///获取到搜索条件
function getPredicate(containerId)
{
    var predicate=new Array();
    var predicateValue=new Array();
    var predicateIndex=0;
    $("#"+containerId+" input[type='text']").each(function () {
        if ($(this).val() && $.trim($(this).val()) != "")
        {
            var operator = $(this).attr("data-operator");
            var name = $(this).attr("data-name");
            var dataType = getDataType($(this));
            var value = $(this).val();

            if (operator && $.trim(operator) != "")
            {
                predicate.push(name + operator+'=@' + predicateIndex);
            }
            else {
                predicate.push(name + '==@' + predicateIndex);
            }
            predicateValue.push("[" + dataType + "#0x2#b91#" + value + " ]");
            predicateIndex++;
        }
    });

    $("#" + containerId + " input[type='checkbox']:checked").each(function () {
        if ($(this).val() && $.trim($(this).val()) != "") {

            var operator = $(this).attr("data-operator");
            var name = $(this).attr("data-name");
            var value = $(this).val();

            var dataType = getDataType($(this));
            if (operator && $.trim(operator) != "") {
                predicate.push(name + operator + '=@' + predicateIndex);
            }
            else {
                predicate.push(name + '==@' + predicateIndex);
            }
            predicateValue.push("[" + dataType + "#0x2#b91#" + value + " ]");
            predicateIndex++;
        }
    });

    $("#" + containerId + " select").each(function () {

        var name = $(this).attr("data-name");
        var value = $('option:selected', $(this)).val();
        var operator = $(this).attr("data-operator");
        var dataType = getDataType($(this));
        if (value && $.trim(value) != "") {
            if (operator && $.trim(operator) != "") {
                predicate.push(name + operator + '=@' + predicateIndex);
            }
            else {
                predicate.push(name + '==@' + predicateIndex);
            }
            predicateValue.push("[" + dataType + "#0x2#b91#" + value + " ]");
            predicateIndex++;
        }
    });
    var predicateFilter = "";
    if (predicate.length > 0 && predicateValue.length > 0) {
        predicateFilter = "Predicate=" + predicate.join("and") + "&PredicateValue=" + encodeURIComponent(predicateValue.join("#0x4#f73#")); 
    }
    else {
        predicateFilter = "Predicate=0xd8d56cce&PredicateValue=''";
    }
    return   predicateFilter;
}


//获取到字段的类型
function getDataType(element)
{
    if (!element) { return "s"; };
    var datatype = element.attr("data-type");
    if (!datatype){
        datatype = "s";
    }
    return datatype;
}


//序列化JqGrid的值
function getSerializerJqGrid(gridId) {
    var tmp = [];
    $("#" + gridId + ">tbody>tr").each(function () {
        if (this.id) {
            tmp[this.id] = $("#" + gridId).getRowData(this.id);
        }
    });
    return tmp;
}


function SysCommon()
{

}
//获取搜索条件
SysCommon.prototype.getPredicate=function()
{
    return getPredicate();
}
//序列化JqGrid的值
SysCommon.prototype.getSerilizerJgGrid = function (gridId)
{
    return getSerializerJqGrid(gridId);
}


